package com.huang.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.auth.converter.AuthRoleVOConverter;
import com.huang.auth.entity.po.AuthRolePO;
import com.huang.auth.entity.request.AuthRoleAddRequest;
import com.huang.auth.entity.request.AuthRoleEditRequest;
import com.huang.auth.entity.request.AuthRoleQueryRequest;
import com.huang.auth.entity.vo.AuthRoleInfoVO;
import com.huang.auth.entity.vo.AuthRoleSimpleVO;
import com.huang.auth.entity.vo.AuthRoleVO;
import com.huang.auth.service.AuthRoleService;
import com.huang.entity.result.HPage;
import com.huang.entity.result.HPageResult;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HuangShen
 * @Description 角色
 * @create 2021-11-13 14:59
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/backend")
public class AuthRoleController {


    @Resource
    private AuthRoleService authRoleService;

    @Resource
    private AuthRoleVOConverter authRoleVOConverter;

    @ApiOperation(value = "角色分页条件查询")
    @PostMapping("/roles")
    public HPageResult<AuthRoleVO> getRoleList(@RequestBody AuthRoleQueryRequest authRoleQueryRequest){
        Page<AuthRolePO> roleListPage = this.authRoleService.getRoleList(authRoleQueryRequest);
        List<AuthRoleVO> authRoleVOS = authRoleVOConverter.batchConvert(roleListPage.getRecords());
        HPage page = new HPage();
        page.setPageNo(roleListPage.getCurrent());
        page.setPageSize(roleListPage.getSize());
        page.setAllCount(roleListPage.getTotal());
        page.setTotalPageNum(roleListPage.getPages());
        return ResultBuilder.successHPage(authRoleVOS, page);
    }






    @ApiOperation(value = "添加角色")
    @PostMapping("/role")
    public HStringResult<String> addRole(@RequestHeader("userId") String operatorId, @RequestBody AuthRoleAddRequest authRoleAddRequest) {
        authRoleAddRequest.setCreateUser(Long.parseLong(operatorId));
        if (authRoleService.creatRole(authRoleAddRequest)) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }


    @ApiOperation(value = "根据id更新角色信息")
    @PutMapping("/role")
    public HStringResult<String> updateRoleById(@RequestHeader("userId") String operatorId, @RequestBody AuthRoleEditRequest authRoleEditRequest) {
        authRoleEditRequest.setUpdateUser(Long.parseLong(operatorId));
        if(this.authRoleService.updateRole(authRoleEditRequest)){
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();

    }

    @ApiOperation(value = "根据id删除角色")
    @DeleteMapping("/role")
    @Transactional
    public HStringResult<String> deleteRoleById(@RequestHeader("userId") String operatorId, @RequestParam String roleId) {
        // 1. 删除角色资源表映射关系
        if (this.authRoleService.deleteRoleById(operatorId,roleId)) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    @ApiOperation(value = "角色信息")
    @GetMapping("/role")
    public HStringResult<AuthRoleInfoVO> getRole(@RequestParam String roleId) {
        AuthRoleInfoVO authRoleInfoVO = this.authRoleService.getRoleInfo(Long.getLong(roleId));
        return ResultBuilder.successHSingle(authRoleInfoVO);
    }

    @ApiOperation(value = "简单角色列表")
    @GetMapping("/role/simple")
    public HStringResult<List<AuthRoleSimpleVO>> getRoleSimpleList() {
        List<AuthRoleSimpleVO> authRoleSimpleVOS = this.authRoleService.roleSimpleList();
        return ResultBuilder.successHSingle(authRoleSimpleVOS);
    }


}
